package cn.jilian.trans.bizz.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import cn.jilian.common.dto.PageInfo;
import cn.jilian.common.dto.PageResult;
import cn.jilian.common.dto.SortInfo;
import cn.jilian.dao.OrderDAO;
import cn.jilian.dao.OrderItemDAO;
import cn.jilian.entity.EleOrderItem;
import cn.jilian.entity.EscOrderItem;
import cn.jilian.entity.Order;
import cn.jilian.entity.OrderItem;
import cn.jilian.trans.bizz.dto.TransOrder;
import cn.jilian.trans.bizz.service.OrderService;


@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private OrderDAO orderDAO;
	@Autowired
	private OrderItemDAO orderItemDAO;
	
	@Override
	public List<TransOrder> queryUnpostOrders(long id) {
		
		return null;
	}

	@Override
	public List<OrderItem> queryDetails(long orderId) {
		return orderItemDAO.findByFid(orderId);
	}

	@Override
	public List<OrderItem> queryEscaltors(long orderId) {
		
		return orderItemDAO.findByFidAndMeta(orderId,Long.parseLong(OrderItem.ESC_META));
	}

	@Override
	public List<OrderItem> queryElevators(long orderId) {
		
		return orderItemDAO.findByFidAndMeta(orderId,Long.parseLong(OrderItem.ESC_META));
	}

	@Override
	public int saveItems(Long fid, List<Map<String,Object>> items) {
		int r=0;
		for(Map<String,Object> item:items) {
			item.put("orderId", fid);
//			orderItemEntity.add(item);
		}
		return r;
	}

	

	@Override
	public Order queryOrderBySid(long orderId) {
		Order order=orderDAO.findOne(orderId);
		return order;
	}

	@Override
	public int save(Order trans_order) {
		trans_order=orderDAO.save(trans_order);
		return 1;
	}

	

	@Override
	public long add(Order trans_order) {
		trans_order=orderDAO.save(trans_order);
		return trans_order.getId();
	}

	@Override
	public EleOrderItem add(EleOrderItem item) {
		return orderItemDAO.save(item);
	}

	@Override
	public EscOrderItem add(EscOrderItem item) {
		return orderItemDAO.save(item);
	}

	@Override
	public PageResult<Order> query(Long fid, String name, String startDate, String endDate,Integer flag,
			PageInfo pageInfo,SortInfo sortInfo) {
		int page=((Number)(pageInfo.getStart()/pageInfo.getLimit())).intValue();
		Direction dir=StringUtils.equalsIgnoreCase("DESC", sortInfo.getDirect())?Direction.DESC:Direction.ASC;
		Pageable pageable=new PageRequest(page,pageInfo.getLimit(),dir,sortInfo.getField());
		org.springframework.data.domain.Page<Order> rs=null;
		if(StringUtils.isBlank(name)) {
			if(StringUtils.isBlank(startDate)) {
				if(StringUtils.isBlank(endDate)) {
					rs=orderDAO.findByFidAndFlag(fid, flag, pageable);
				}else {
					rs=orderDAO.findByFidAndFlagAndStartDateLessThanEqual(fid, flag,endDate, pageable);
				}
			}else {
				if(StringUtils.isBlank(endDate)) {
					rs=orderDAO.findByFidAndFlagAndStartDateGreaterThanEqual(fid, flag,startDate, pageable);
				}else {
					rs=orderDAO.findByFidAndFlagAndStartDateLessThanEqual(fid, flag,startDate, pageable);
				}
			}
		}else {
			if(StringUtils.isBlank(startDate)) {
				if(StringUtils.isBlank(endDate)) {
					rs=orderDAO.findByFidAndNameContainingAndFlag(fid, name, flag, pageable);
				}else {
					rs=orderDAO.
							findByFidAndNameContainingAndFlagAndStartDateLessThanEqual(fid,
									name, flag, endDate, pageable);
				}
			}else {
				if(StringUtils.isBlank(endDate)) {
					rs=orderDAO.findByFidAndNameContainingAndFlagAndStartDateGreaterThanEqual(fid, name, flag, startDate, pageable);
				}else {
					rs=orderDAO.findByFidAndNameContainingAndFlagAndStartDateBetween(fid,name,flag,startDate,endDate,pageable);
				}
				
			}
		}
		return new PageResult<Order>(new PageInfo(pageInfo.getStart(),(int)rs.getTotalElements(),pageInfo.getLimit(),rs.getNumberOfElements()),rs.getContent());
		
	}

}
